#include "weather_lable.h"

weather_lable::weather_lable(QWidget *parent) : QLabel(parent)
{
    
}

// 绘制高温和低温的折线图
void weather_lable::paintEvent(QPaintEvent *event)
{
    
    //调用父类绘制（清空背景，避免残留）
        QLabel::paintEvent(event);
    // 计算点 - X
        for (int i = 0 ; i < 7 ; i ++) {
            high_temp_point[i].setX( (this->width() / 8) * (i + 1) );
            low_temp_point[i].setX( (this->width() / 8) * (i + 1) );
        }
        for (int i = 0 ; i < 7 ; i ++) {
            high_temp_point[i].setY( this->height() / 2 );
            low_temp_point[i].setY( this->height() / 2 );
        }
        
        // 计算最大值 和 最小值
        int high_temp_max = 0;
        int low_temp_max = 0;
        int high_temp_min = 500;
        int low_temp_min = 500;
        
        for (int i  = 0; i < 7 ; i ++) {
            high_temp_max = high_temp_max > high_temp[i] ? high_temp_max : high_temp[i];
            low_temp_max = low_temp_max > low_temp[i] ? low_temp_max : low_temp[i];
            high_temp_min = low_temp_min < high_temp[i] ? low_temp_min : high_temp[i];
            low_temp_min = low_temp_min < low_temp[i] ? low_temp_min : low_temp[i];
        }
        
        // 绘制直线
        
        // 中心线坐标
        QPoint Centre_1 , Centre_2;
        Centre_1.setX(high_temp_point[0].x());
        Centre_1.setY(this->height() / 2);
        
        Centre_2.setX(high_temp_point[6].x());
        Centre_2.setY(this->height() / 2);
        
        
        // 计算 Y 轴范围
        int fd_value = high_temp_max - low_temp_min;
        int max_y = this->height() - 20;
        int min_y = 50;
        
        double scale = static_cast<double>(max_y - min_y) / (fd_value);
        
        //计算高温点 y 坐标
        for (int i = 0; i < 7; i++) {
            double offset_x = this->high_temp[i] - high_temp_min;
            int y = max_y - static_cast<int>(offset_x * scale);
            high_temp_point[i].setY(y);
        }
        //计算低温点 y 坐标
        for (int i = 0; i < 7; i++) {
            double offset_y = this->low_temp[i] - low_temp_min;
            int y = max_y - static_cast<int>(offset_y * scale);
            low_temp_point[i].setY(y);
        }
        
    
        // 实例化画布
        QPainter Painter(this);
        Painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿，让曲线更平滑
        // 实例化画笔
        QPen Pen;
        // 实例化画刷
        QBrush Brush;
        // 实例化路径
        QPainterPath Path_1;
        QPainterPath Path_2;
        
        
        // 设置温度曲线
        
        
        // ==================== 高温 ====================
        // 画笔设置
        Pen.setColor(QColor(253 , 17,  0,  255));
        Pen.setStyle(Qt::DotLine);
        Painter.setPen(Pen);
        
        // 起始
        Path_1.moveTo(high_temp_point[0]);
        Path_1.lineTo(high_temp_point[1]);
        
        // 绘制
        Painter.drawPath(Path_1);
        
        // 设置笔
        Pen.setStyle(Qt::SolidLine);
        Painter.setPen(Pen);
        
        // 起始
        Path_2.moveTo(high_temp_point[1]);
        
        Path_2.lineTo(high_temp_point[2]);
        Path_2.lineTo(high_temp_point[3]);
        Path_2.lineTo(high_temp_point[4]);
        Path_2.lineTo(high_temp_point[5]);
        Path_2.lineTo(high_temp_point[6]);
        
        // 绘制
        Painter.drawPath(Path_2);   
        
        
        // 绘制圆点
        
        Pen.setColor(QColor(255 , 170,  0,  255));
        Pen.setStyle(Qt::SolidLine);
        Painter.setPen(Pen);
        
        Brush.setColor(QColor(255 , 170,  0,  255));
        Brush.setStyle(Qt::SolidPattern);
        Painter.setBrush(Brush);
        
        // 绘制圆点
        for ( int i  = 0 ; i < 7; i++) {
            Painter.drawEllipse( high_temp_point[i].x() - 5 , high_temp_point[i].y() - 5 , 10 , 10 );
            Painter.drawText( high_temp_point[i].x() - 3, high_temp_point[i].y() - 10  , QString::number(high_temp[i] ));
       }
        
        Painter.setBrush(Qt::NoBrush);
    
    
    // ==================== 低温曲线 ====================
        QPainterPath lowPath1, lowPath2;
        // 画笔设置
          Pen.setColor(QColor(31 , 105,  224,  255));
          Pen.setStyle(Qt::DotLine);
          Painter.setPen(Pen);
          
          // 起始
          lowPath1.moveTo(low_temp_point[0]);
          lowPath1.lineTo(low_temp_point[1]);
          
          // 绘制
          Painter.drawPath(lowPath1);
          
          // 设置笔
          Pen.setStyle(Qt::SolidLine);
          Painter.setPen(Pen);
          
          // 起始
          lowPath2.moveTo(low_temp_point[1]);
          
          lowPath2.lineTo(low_temp_point[2]);
          lowPath2.lineTo(low_temp_point[3]);
          lowPath2.lineTo(low_temp_point[4]);
          lowPath2.lineTo(low_temp_point[5]);
          lowPath2.lineTo(low_temp_point[6]);
          
          // 绘制
          Painter.drawPath(lowPath2);   
          
          
          // 绘制圆点
          
          Pen.setColor(QColor(31 , 105,  224,  255));
          Pen.setStyle(Qt::SolidLine);
          Painter.setPen(Pen);
          
          Brush.setColor(QColor(31 , 105,  224,  255));
          Brush.setStyle(Qt::SolidPattern);
          Painter.setBrush(Brush);
          
          // 绘制圆点
          for ( int i  = 0 ; i < 7; i++) {
              Painter.drawEllipse( low_temp_point[i].x() - 5 , low_temp_point[i].y() - 5 , 10 , 10 );
              Painter.drawText( low_temp_point[i].x() - 3, low_temp_point[i].y() - 10  , QString::number(low_temp[i] ));
          }
          
    
}

void weather_lable::get_weather_temp(QList<Forecast> temp)
{
    if(temp.length() < 7)
    {
        qDebug() << "天气数据不足，至少需要7天数据";
        return;
    }
    
    // 提取温度数据
    for (int i = 0; i < 7; i++) {
        // 提取高温数据
        QString highStr = temp[i].high;
        highStr = highStr.remove(QRegExp("[^0-9\\.]"));
        bool ok;
        this->high_temp[i] = highStr.toInt(&ok);
        if (!ok) {
            qDebug() << "高温数据转换失败:" << temp[i].high;
            this->high_temp[i] = 0;
        }
        
        // 提取低温数据
        QString lowStr = temp[i].low;
        lowStr = lowStr.remove(QRegExp("[^0-9\\.]"));
        this->low_temp[i] = lowStr.toInt(&ok);
        if (!ok) {
            qDebug() << "低温数据转换失败:" << temp[i].low;
            this->low_temp[i] = 0;
        }
    }
    
    // 请求重绘
    update();
}
